package zeus.ejb;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import zeus.exception.LoginException;
import zeus.model.Administrador;
import zeus.model.Cliente;
import zeus.service.EjbLoginService;


@Stateless
public class EjbLogin implements EjbLoginService{
	
	@PersistenceContext
	EntityManager entityManager;
	
	@Override
	public Cliente loginCliente(String email, String senha) throws LoginException{
		
		String hql = "from Cliente c where c.email='" + email + "' and c.senha= '" + senha + "'";
		
		
		Query query = entityManager.createQuery(hql);
		
		Cliente cliente = null;
		
		try{
			cliente = (Cliente) query.getSingleResult();
		}catch(Exception e){
			throw new LoginException();
		}
		
		return cliente;
	}
	
	@Override
	public Administrador loginAdministrador(String chave, String senha) throws LoginException{
		
		String hql = "from Administrador a where a.chave ='" + chave + "' and a.senha= '" + senha + "'";
		
		Query query = entityManager.createQuery(hql);
		
		Administrador administrador = null;
		
		try{
			administrador = (Administrador) query.getSingleResult();
		}catch(Exception e){
			throw new LoginException();
		}
		
		return administrador;
		
	}

}
